#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
# sys.path.append('/opt/work/caffe/python')
sys.path.insert(0, '.')

import random
import argparse
import os
import numpy as np
from collections import OrderedDict
import struct
import cv2
import math
from math import cos, sin
import re
from matplotlib import pyplot as plt

# SLEEP_TIME = 40
SLEEP_TIME = 1

image_base_path = r'/rootfs/media/kasim/Data1/data/VideoCropFace'
ok_list_file = r'/rootfs/media/kasim/Data1/data/VideoCropFace/all/face_image_q{}-{}_filter_filter_rec_ok.txt'
err_list_file = r'/rootfs/media/kasim/Data1/data/VideoCropFace/all/face_image_q{}-{}_filter_filter_rec_err.txt'
device_id_set = {
    # '150100414a54443452067fa1d4c56600',  #  64  94 68.09% 越秀星汇隽庭 6(幢/座) 3(单元) 货梯梯 A面
    # '150100414a5444345203bccb439eb500',  # 197 319 61.76% 天水湖 1(幢/座) 1(单元) 2梯 A面
    # '150100414a54443452064d2c43e43600',  #  91 172 52.91% 大信时尚家园二期 4(幢/座) 1(单元) 1梯 A面
    # '150100414a54443452064d2743243600',  #  23  51 45.10% 天水湖 3(幢/座) 1(单元) 1梯 A面
    # '150100414a54443452067fa6d45f6600',  #  74 169 43.79% 天水湖 4(幢/座) 1(单元) 1梯 A面
    # '150100414a5444345203bcd04287b500',  # 176 411 42.82% 越秀星汇隽庭 3(幢/座) 1(单元) 1梯 A面
    # '150100414a54443452064d2742c43600',  # 159 376 42.29% 溢彩家园 2(幢/座) 1(单元) 2梯 A面
    # '150100414a54443452067fa1d4cd6600',  # 235 560 41.96% 越秀星汇隽庭 4(幢/座) 1(单元) 1梯 A面
    # '150100414a54443452067fa6d4a36600',  # 107 255 41.96% 名雅学府 1(幢/座) 1(单元) 1梯 A面
    # '150100414a54443452067fa6d4b06600',  #  52 130 40.00% 越秀星汇隽庭 5(幢/座) 1(单元) 1梯 A面

    # '150100414a54443452067fa6d4556600',  # 104 529 19.66% 越秀星汇隽庭 7(幢/座) 1(单元) 1梯 A面
    # '150100414a54443452067fa1d4096600',  #  76 391 19.44% 越秀星汇隽庭 4(幢/座) 1(单元) 2梯 A面
    # '150100414a54443452064d27432e3600',  #  11  64 17.19% 大信时尚家园三期 12(幢/座) 1(单元) 1梯 A面
    # '150100414a54443452064d2c43e13600',  #  33 198 16.67% 大信时尚家园二期 1(幢/座) 1(单元) 1梯 A面
    # '150100414a5444345203bccb439fb500',  #  37 261 14.18% 越秀星汇隽庭 5(幢/座) 1(单元) 2梯 A面
    # '150100414a54443452064d2c43ed3600',  #  70 496 14.11% 大信时尚家园三期 9(幢/座) 1(单元) 1梯 A面
    # '150100414a54443452064d2742c73600',  #  52 370 14.05% 大信时尚家园二期 5(幢/座) 1(单元) 1梯 A面
    # '150100414a54443452067fa6d45c6600',  #  51 399 12.78% 星光礼寓 7(幢/座) 1(单元) 1梯 A面

    '150100414a54443452067fa6d4586600',  # 65 672 9.67% 越秀星汇隽庭 2(幢/座) 1(单元) 2梯 A面
    '150100414a54443452067fa1d4c16600',  # 29 336 8.63% 星光礼寓 5(幢/座) 1(单元) 2梯 A面
    '150100414a54443452064d2742ee3600',  # 15 211 7.11% 越秀星汇隽庭 6(幢/座) 2(单元) 1梯 A面
    '150100414a54443452067fa6d4a86600',  # 16 276 5.80% 星光礼寓 8(幢/座) 1(单元) 2梯 A面
    '150100414a54443452064d27433e3600',  # 18 358 5.03% 大信时尚家园二期 6(幢/座) 1(单元) 1梯 A面
    '150100414a54443452064d2743233600',  # 30 615 4.88% 大信时尚家园三期 11(幢/座) 1(单元) 1梯 A面
    '150100414a5444345203bcd0428ab500',  # 12 343 3.50% 星光礼寓 5(幢/座) 1(单元) 1梯 A面
    '150100414a54443452064d2742e33600',  # 10 298 3.36% 大信时尚家园三期 13(幢/座) 1(单元) 1梯 A面
    '150100414a54443452067fa6d4ad6600',  #  7 234 2.99% 星光礼寓 8(幢/座) 1(单元) 1梯 A面
    '150100414a54443452067fa6d44b6600',  # 25 843 2.97% 越秀星汇隽庭 1(幢/座) 1(单元) 2梯 A面
    '150100414a54443452067fa6d4ab6600',  # 14 517 2.71% 星光礼寓 6(幢/座) 1(单元) 1梯 A面
    '150100414a54443452064d2742d03600',  # 18 689 2.61% 溢彩家园 1(幢/座) 1(单元) 2梯 A面

    '150100414a54443452067fa6d4bc6600',   # 6 384 1.56% 星光礼寓 6(幢/座) 1(单元) 2梯 A面
    '150100414a5444345203bcd0428cb500',   # 8 611 1.31% 溢彩家园 4(幢/座) 1(单元) 1梯 A面
    '150100414a5444345203bcd04294b500',   # 6 518 1.16% 星光礼寓 7(幢/座) 1(单元) 2梯 A面
    '150100414a54443452064d2743283600',   # 6 586 1.02% 溢彩家园 5(幢/座) 1(单元) 1梯 A面
    '150100414a54443452064d2743383600',   # 3 433 0.69% 名雅居 3(幢/座) 1(单元) 1梯 A面
    '150100414a54443452067fa6d4be6600',   # 3 495 0.61% 越秀星汇隽庭 3(幢/座) 1(单元) 2梯 A面
    '150100414a54443452064d2742ca3600',   # 0   8 0.00% 天水湖 2(幢/座) 1(单元) 1梯 A面
    '150100414a5444345203bcd04293b500',   # 0 222 0.00% 名雅居 1(幢/座) 1(单元) 1梯 A面
    '150100414a54443452067fa1d40a6600',   # 0 298 0.00% 名雅居 2(幢/座) 1(单元) 1梯 A面
    '150100414a54443452067fa1d40e6600',   # 0 554 0.00% 越秀星汇隽庭 7(幢/座) 1(单元) 2梯 A面
    '150100414a54443452067fa6d45b6600',   # 0 677 0.00% 溢彩家园 3(幢/座) 1(单元) 1梯 A面
}


def main():
    quality_interval = 5
    scale = 100
    bin_count = scale // quality_interval

    for quality_index in range(bin_count):
        quality_start = quality_index * quality_interval
        quality_end = quality_start + quality_interval
        ok_list_file_path = ok_list_file.format(quality_start, quality_end)
        ok_file_set = set()
        with open(ok_list_file_path, 'r') as file:
            for line in file.readlines():
                file_name = line.split()[0].strip()
                if device_id_set is not None:
                    device_id = file_name.split('/')[-4]
                    if device_id not in device_id_set:
                        continue
                ok_file_set.add(file_name)

        err_list_file_path = err_list_file.format(quality_start, quality_end)
        err_file_set = set()
        with open(err_list_file_path, 'r') as file:
            for line in file.readlines():
                file_name = line.split()[0].strip()
                if device_id_set is not None:
                    device_id = file_name.split('/')[-4]
                    if device_id not in device_id_set:
                        continue
                err_file_set.add(file_name)
        ok_file_count = len(ok_file_set)
        err_file_count = len(err_file_set)
        total_count = ok_file_count + err_file_count
        if total_count > 0:
            print('{} {} {} {} {}'.format(quality_start, quality_end, err_file_count, ok_file_count, 100*err_file_count/total_count))
        else:
            print('{} {} {} {} {}'.format(quality_start, quality_end, err_file_count, ok_file_count, 0))

    print('Finish!')


if __name__ == '__main__':
    main()
